const express = require('express');
const path = require("path");
const router = express.Router();
const {
	getTemplates,
	saveTemplates
} = require("../db/templatesdb.js");
const {
	getEmailsById,
	addEmailToList,
	markEmailAsRead
} = require("../db/emailsdb.js");
const {
	getFriendMessage
} = require("../db/usersdb.js");




// 获取控制台页面
router.get('/socket', function(req, res, next) {
	// 获取socket测试页面
	res.sendFile(path.join(path.dirname(__dirname), 'public', 'html/imitate.html'));
});

// 获取测试模板
router.get('/template', async function(req, res, next) {
	// 获取所有的测试模板
	let rls = await getTemplates();
	// 将模板传输给前端
	res.json(rls.getMessage());
});


router.post('/template', async function(req, res, next) {
	// 获取所有的测试模板
	let rls = await saveTemplates(req.body);
	// 将模板传输给前端
	res.json(rls.getMessage());
});

// 请求服务器返回所有的当前连接的session信息
router.get('/showSession', async function(req, res, next) {
	// console.log(req.session);
	console.log('短链接ID');
	console.log(req.sessionID);
	console.log(req.session);
	res.json(req.session);
});


// Emil测试接口

// 获取指定_id的所有邮件
router.get('/getEmailsByOwnerId', async function(req, res, next) {
	let {
		id
	} = req.query;
	let rls = await getEmailsByOwnerId(id);
	res.json(rls);
});

// 往指定用户的邮件数组中添加一封邮件的方法
router.post('/addEmailToList', async function(req, res, next) {
	let {
		id
	} = req.body;
	console.log("id");
	console.log(id);

	let obj = {
		"sendID": "66c367bd0e4d972cf4b77583",
		"receiveID": "66c3682e0e4d972cf4b77586",
		"message": "邮件消息1",
		"room": "",
		"sendType": "PC",
		"time": "2024-08-21T09:00:00Z",
		"notes": false,
		"read": true,
		"result": "close"
	}


	let rls = await addEmailToList(id, obj);
	res.json(rls);
});

// 根据id删除指定邮件的方法
router.post('/markEmailAsRead', async function(req, res, next) {
	let {
		id,
		emailId
	} = req.body;

	let rls = await markEmailAsRead(id, emailId);
	res.json(rls);
});

// 根据查询id，查询好友列表和好友的相关信息
router.get('/getFriendMessage', async function(req, res, next) {
	let {
		findID
	} = req.query;
	let rls = await getFriendMessage(findID);
	res.json(rls);
});







module.exports = router;